import 'package:flutter/material.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/water_click.dart';

///Title:单行文本 <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-01 <br/>
///version 1.0 <br/>
class SingleLineItem extends StatelessWidget {
  ///图片尺寸
  final double imageSize;

  ///左图片
  final String leftImage;

  ///左文本
  final String leftText;

  ///右文本
  final String rightText;

  ///hint
  final String hint;

  ///右箭头
  final bool showArrow;

  ///下划线
  final bool showUnderLine;

  ///是否可编辑
  final bool editable;

  ///点击回调
  final VoidCallback onPressed;

  SingleLineItem({
    Key key,
    this.leftImage,
    this.imageSize = 25,
    this.leftText = "",
    this.rightText = "",
    this.hint = "",
    this.showArrow = true,
    this.showUnderLine = true,
    this.onPressed,
    this.editable = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool _showArrow;
    //可编辑
    if (editable) {
      _showArrow = showArrow ?? editable;
    } else {
      _showArrow = false;
    }

    return WaterClick(
      padding: Paddings.common,
      minHeight: 45,
      enable: editable,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            width: showUnderLine ? 0.5 : 0.0,
            color: CommonColors.common_divide_line,
          ),
        ),
      ),
      onPressed: onPressed,
      child: Row(
        children: <Widget>[
          Utils.strIsNotEmpty(leftImage) ? _leftImage() : Gaps.empty,
          _leftText(),
          Gaps.h_gap_10,
          Expanded(child: Utils.strIsEmpty(rightText) ? _hintText() : _rightText()),
          Gaps.h_gap_10,
          Offstage(
            offstage: !_showArrow,
            child: _rightArrow(),
          ),
        ],
      ),
    );
  }

  Widget _leftImage() {
    return Container(
      margin: EdgeInsets.only(right: 10),
      child: Image.asset(
        leftImage,
        width: imageSize,
        height: imageSize,
      ),
    );
  }

  Widget _leftText() {
    Widget textWidget = Text(
      leftText,
      style: TextStyle(
        color: CommonColors.common_text,
        fontSize: CommonDimens.font_size_middle,
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );

    return Utils.strIsEmpty(rightText)
        ? Expanded(flex: 2, child: textWidget)
        : Container(
            width: 120,
            child: textWidget,
          );
  }

  Widget _rightText() {
    return Container(
      alignment: Alignment.centerRight,
      child: Text(
        rightText,
        style: TextStyle(
          color: CommonColors.gray,
          fontSize: CommonDimens.font_size_middle,
        ),
        maxLines: 3,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  Widget _hintText() {
    String _hint;
    if (editable) {
      _hint = hint;
    } else {
      _hint = "";
    }

    return Container(
      alignment: Alignment.centerRight,
      child: Text(
        _hint,
        style: TextStyle(
          color: CommonColors.gray,
          fontSize: CommonDimens.font_size_middle,
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  Widget _rightArrow() {
    return Image.asset(
      R.assetsImgCommonIconRightArrow,
      width: 17,
      height: 17,
    );
  }
}
